<template>
  <div class="app-container">
    <el-tabs tab-position="left" style="">
      <el-tab-pane label="商户管理">
        <div v-show="detail == false">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="商户名" prop="businessName">
              <el-input v-model="queryParams.businessName" placeholder="请输入商户名" clearable
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="queryParams.address" placeholder="请输入详细地址" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                v-hasPermi="['shop:shop:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                v-hasPermi="['shop:shop:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                v-hasPermi="['shop:shop:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                v-hasPermi="['shop:shop:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="主键" align="center" prop="id" />
            <el-table-column label="商户名" align="center" prop="businessName" />
            <el-table-column label="详细地址" align="center" prop="address" />
            <el-table-column label="负责人" align="center" prop="responsibleName" />
            <el-table-column label="注册时间" align="center" prop="creatTime" width="180">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.creatTime, '{y}-{m}-{d}') }}</span>
              </template>
            </el-table-column>
            <el-table-column label="审核状态" align="center" prop="audit">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.che_shop_audit" :value="scope.row.audit" />
              </template>
            </el-table-column>

            <!-- <el-table-column label="负责人手机" align="center" prop="phone" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleOpen(scope.row)">详情</el-button>
                <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shop:shop:remove']"
          >删除</el-button> -->
                <el-button v-if="scope.row.audit == 0" size="mini" type="text" icon="el-icon-edit"
                  @click="handleAudit(scope.row)" v-hasPermi="['shop:shop:edit']">审核</el-button>
              </template>
            </el-table-column>
          </el-table>

          <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

          <!-- 添加或修改商户管理对话框 -->
          <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-form-item label="商户名" prop="businessName">
                <el-input v-model="form.businessName" placeholder="请输入商户名" />
              </el-form-item>
              <el-form-item label="营业执照号码" prop="number">
                <el-input v-model="form.number" placeholder="请输入营业执照号码" />
              </el-form-item>
              <el-form-item label="经营区域" prop="area">
                <el-input v-model="form.area" placeholder="请输入经营区域" />
              </el-form-item>
              <el-form-item label="详细地址" prop="address">
                <el-input v-model="form.address" placeholder="请输入详细地址" />
              </el-form-item>
              <el-form-item label="商家介绍" prop="intro">
                <el-input v-model="form.intro" placeholder="请输入商家介绍" />
              </el-form-item>
              <el-form-item label="审核状态" prop="audit">
                <el-radio-group v-model="form.audit">
                  <el-radio v-for="dict in dict.type.che_shop_audit" :key="dict.value" :label="parseInt(dict.value)">{{
                    dict.label }}</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="注册时间" prop="creatTime">
                <el-date-picker clearable v-model="form.creatTime" type="date" value-format="yyyy-MM-dd"
                  placeholder="请选择注册时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="门面照片" prop="shopImage">
                <image-upload v-model="form.shopImage" />
              </el-form-item>
              <el-form-item label="营业执照" prop="licence">
                <el-input v-model="form.licence" placeholder="请输入营业执照" />
              </el-form-item>
              <el-form-item label="身份证正面" prop="cardFront">
                <el-input v-model="form.cardFront" placeholder="请输入身份证正面" />
              </el-form-item>
              <el-form-item label="身份证反面" prop="cardBack">
                <el-input v-model="form.cardBack" placeholder="请输入身份证反面" />
              </el-form-item>
              <el-form-item label="余额" prop="money">
                <el-input v-model="form.money" placeholder="请输入余额" />
              </el-form-item>
              <el-form-item label="负责人手机" prop="phone">
                <el-input v-model="form.phone" placeholder="请输入负责人手机" />
              </el-form-item>
              <el-form-item label="负责人姓名" prop="responsibleName">
                <el-input v-model="form.responsibleName" placeholder="请输入负责人姓名" />
              </el-form-item>
              <el-form-item label="密码" prop="password">
                <el-input v-model="form.password" placeholder="请输入密码" />
              </el-form-item>
              <el-form-item label="负责人身份证号" prop="cardid">
                <el-input v-model="form.cardid" placeholder="请输入负责人身份证号" />
              </el-form-item>
              <el-form-item label="店名" prop="shopName">
                <el-input v-model="form.shopName" placeholder="请输入店名" />
              </el-form-item>
              <el-form-item label="每周营业时间" prop="week">
                <el-input v-model="form.week" placeholder="请输入每周营业时间" />
              </el-form-item>
              <el-form-item label="开始营业时间" prop="startTime">
                <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
                  placeholder="请选择开始营业时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="结束营业时间" prop="endTime">
                <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
                  placeholder="请选择结束营业时间">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="经度" prop="longitude">
                <el-input v-model="form.longitude" placeholder="请输入经度" />
              </el-form-item>
              <el-form-item label="纬度" prop="latitude">
                <el-input v-model="form.latitude" placeholder="请输入纬度" />
              </el-form-item>
              <el-form-item label="门店总星级评分" prop="rate">
                <el-input v-model="form.rate" placeholder="请输入门店总星级评分" />
              </el-form-item>
              <el-form-item label="门店总销售数量" prop="total">
                <el-input v-model="form.total" placeholder="请输入门店总销售数量" />
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
            </div>
          </el-dialog>
          <!-- 审核对话框 -->
          <el-dialog title="审核" :visible.sync="dialogFormVisible" width="440px">
            <el-form :model="formAudit">
              <el-form-item label="审核结果" label-width="80px">
                <el-radio-group v-model="formAudit.audit">
                  <el-radio :label="1">审核通过</el-radio>
                  <el-radio :label="2">审核不通过</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="审核说明" label-width="80px">
                <el-input type="textarea" v-model="formAudit.desc"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="dialogFormVisible = false">取 消</el-button>
              <el-button type="primary" @click="confirm">确 定</el-button>
            </div>
          </el-dialog>
        </div>
        <div v-show="detail == true">
          <div class="header">
            <div class="header-left" style="float: left;">
              <i class="el-icon-s-platform"></i>
              商户信息
            </div>
            <div class="header-right" style="float: right;">
              <el-button type="primary" @click="back">返回</el-button>
            </div>
          </div>
          <div>
            <el-card class="box-card" shadow="never">
              <div slot="header" class="clearfix">
                <span>商户名称</span>
              </div>
              <div>
                <table border="1" borderColor="#e0e0e0" rules="all" style="font-size: 14px;">
                  <tr height="40" style="text-align: center;">
                    <td style="width: 150px;background-color: #eee;">商户名称:</td>
                    <td style="width: 400px;">{{ shop.businessName }}</td>
                    <td style="width: 150px;background-color: #eee;">营业执照编号:</td>
                    <td style="width: 400px;">{{ shop.number }}</td>
                  </tr>
                  <tr height="40" style="text-align: center;">
                    <td style="background-color: #eee;">负责人:</td>
                    <td>{{ shop.responsibleName }}</td>
                    <td style="background-color: #eee;">负责人手机号:</td>
                    <td>{{ shop.phone }}</td>
                  </tr>
                  <tr height="40" style="text-align: center;">
                    <td style="background-color: #eee;">身份证号码:</td>
                    <td>{{ shop.cardid }}</td>
                    <td style="background-color: #eee;">经营区域:</td>
                    <td>410324903030</td>
                  </tr>
                  <tr height="40" style="text-align: center;">
                    <td style="background-color: #eee;">注册时间:</td>
                    <td>{{ shop.creatTime }}</td>
                    <td style="background-color: #eee;">审核状态:</td>
                    <td>
                      <el-tag v-if="shop.audit == 0" type="success">未审核</el-tag>
                      <el-tag v-if="shop.audit == 1" type="success">已通过</el-tag>
                      <el-tag v-else-if="shop.audit == 2" type="danger">已拒绝</el-tag>
                    </td>
                  </tr>
                  <tr height="40" style="text-align: center;">
                    <td style="background-color: #eee;">详细地址:</td>
                    <td colspan="3">{{ shop.address }}</td>
                  </tr>
                  <tr height="40" style="text-align: center;">
                    <td style="background-color: #eee;">商家介绍:</td>
                    <td colspan="3">{{ shop.intro }}</td>
                  </tr>
                </table>
              </div>
            </el-card>
            <el-card class="box-card" shadow="never">
              <div slot="header" class="clearfix">
                <span>商户照片</span>
              </div>
              <div style="font-size: 14px;">
                <div style="float: left;">
                  <div style="float: left;">
                    门店照片
                  </div>
                  <image-preview style="margin-top: 25px;margin-left:15px;" :src="shop.shopImage" :width="200"
                    :height="150" />
                </div>
                <div style="float: left;margin-left: 15px;">
                  <div style="float: left;">
                    营业执照
                  </div>
                  <image-preview style="margin-top: 25px;margin-left:15px;" :src="shop.licence" :width="200"
                    :height="150" />
                </div>
                <div style="float: left;margin-left: 15px;">
                  <div style="float: left;">
                    身份证正反面图片
                  </div>
                  <image-preview style="margin-top: 25px;margin-left:15px;" :src="shop.cardFront" :width="200"
                    :height="150" />
                </div>

                <div style="float: left;margin-left: 35px;margin-top: 25px;">
                  <image-preview :src="shop.cardBack" :width="200" :height="150" />
                </div>

              </div>
            </el-card>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="商户排名">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>商户排名</span>
          </div>
          <div>
            <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'rate', order: 'descending'}">
              <el-table-column label="排名" align="center" type="index"  />
              <el-table-column label="商户名" align="center" prop="businessName" />
              <el-table-column label="详细地址" align="center" prop="address" />
              <el-table-column label="负责人" align="center" prop="responsibleName" />
              <el-table-column label="平均星级" align="center" prop="rate">
                <template scope="scope">
                  <el-rate disabled v-model="scope.row.rate"></el-rate>
                </template>

              </el-table-column>
            </el-table>

            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize" @pagination="getList" />
          </div>
        </el-card>


      </el-tab-pane>

    </el-tabs>

  </div>
</template>

<script>
import { listShop, getShop, delShop, addShop, updateShop } from "@/api/shop/shop";

export default {
  name: "Shop",
  dicts: ['che_shop_audit'],
  data() {
    return {
      //详情数据
      shop: {},
      detail: false,
      //审核对话框
      dialogFormVisible: false,
      // 审核表单
      formAudit: {
        audit: ''
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商户管理表格数据
      shopList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessName: null,
        address: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {

    //详情页面返回按钮
    back() {
      this.detail = false;
    },
    handleOpen(row) {
      console.log(row);
      this.shop = row;
      this.detail = true;
    },
    /** 查询商户管理列表 */
    getList() {
      this.loading = true;
      listShop(this.queryParams).then(response => {
        this.shopList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessName: null,
        number: null,
        area: null,
        address: null,
        intro: null,
        audit: null,
        creatTime: null,
        shopImage: null,
        licence: null,
        cardFront: null,
        cardBack: null,
        money: null,
        phone: null,
        responsibleName: null,
        password: null,
        cardid: null,
        shopName: null,
        week: null,
        startTime: null,
        endTime: null,
        longitude: null,
        latitude: null,
        rate: null,
        total: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShop(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商户管理";
      });
    },
    handleAudit(row) {
      this.dialogFormVisible = true;
      this.formAudit.id = row.id;
    },
    confirm() {
      this.dialogFormVisible = true;
      updateShop(this.formAudit).then(response => {
        this.$modal.msgSuccess("审核成功");
        this.dialogFormVisible = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShop(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商户管理编号为"' + ids + '"的数据项？').then(function () {
        return delShop(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('shop/shop/export', {
        ...this.queryParams
      }, `shop_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.header {
  height: 50px;
  /* border: 1px solid #000; */
}
</style>
